package com.yima.baseproj.module.app.equipmentDevice;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yima.baseproj.config.exception.type.ApiException;
import com.yima.baseproj.module.app.equipmentEquipment.EquipmentEquipmentEntity;
import com.yima.baseproj.module.app.equipmentEquipment.EquipmentEquipmentService;
import com.yima.baseproj.module.config.BaseController;
import com.yima.baseproj.module.config.utils.PageUtil;
import com.yima.baseproj.module.config.utils.PageVo;
import com.yima.baseproj.module.app.equipmentDevice.model.*;
import com.yima.baseproj.security.SecurityUtil;
import com.yima.baseproj.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import lombok.extern.slf4j.Slf4j;


/**
 * @author 一码
 * @createDate 2024-01-06 23:35:58
 * @contact 1424529270@qq.com
 **/
@Slf4j
@Api(tags = "回调数据")
@RestController
@RequestMapping("api/equipment/device")
public class EquipmentDeviceController extends BaseController {
    @Resource
    private EquipmentEquipmentService equipmentEquipmentService;
    @Resource
    private EquipmentDeviceService equipmentDeviceService;

    @ApiOperation("查询分页")
    @GetMapping(value = "/findAllByPage")
    public R<Object> findAllByPage(PageVo page, EquipmentDeviceSearchModel searchVo) {
        String openId = SecurityUtil.getUserId();
        if (openId != null && !openId.isEmpty()) {
            List<EquipmentEquipmentEntity> openList = equipmentEquipmentService.selectList(openId);
            boolean b = openList.stream().anyMatch(e -> Objects.equals(e.getDeviceno(), searchVo.getDeviceNo()));
            if (b == true) {
                Page<Map> pageData = equipmentDeviceService.findByPage(PageUtil.initMpPage(page), searchVo);
                System.out.println("pageData: " + pageData);
                return R.success(pageData, "操作成功");
            }
        }
        return R.error("未找到数据");
    }


    @ApiOperation("查询当日")
    @GetMapping(value = "/InquireDay")
    public R<MonthlyModel> InquireDay(String deviceNo) {
        MonthlyModel day = equipmentDeviceService.day(deviceNo);
        return R.success(day, "操作成功");
    }

    @ApiOperation("查询每月")
    @GetMapping(value = "/InquireMonthly")
    public R<MonthlyModel> InquireMonthly(String deviceNo) {
        MonthlyModel monthly = equipmentDeviceService.monthly(deviceNo);
        return R.success(monthly, "操作成功");
    }


    @ApiOperation("查询所有分页")
    @GetMapping(value = "/findAllPage")
    public R<Object> findAllPage(PageVo page, EquipmentDeviceSearchModel searchVo) {
//        String deviceName = MD5.splitList(searchVo.getDeviceName());
//        searchVo.setDeviceName(deviceName);
        Page<Map> pageData = equipmentDeviceService.findByPage(PageUtil.initMpPage(page), searchVo);
        System.out.println("pageData: " + pageData);
        return R.success(pageData, "操作成功");
    }

    @ApiOperation("查询列表")
    @GetMapping(value = "/findAllByList")
    public R<Object> findAllByList(EquipmentDeviceSearchModel searchVo) {
        List<Map> list = equipmentDeviceService.findByList(searchVo);
        return R.success(list, "操作成功");
    }

    @ApiOperation("查询详情")
    @GetMapping(value = "/findById/{id}")
    public R<Map> findById(@PathVariable String id) {
        EquipmentDeviceEntity equipmentDeviceEntity = equipmentDeviceService.getById(id);
        if (equipmentDeviceEntity == null) {
            throw ApiException.getInstance("数据不存在，请刷新重试");
        }

        Map result = JSON.parseObject(JSON.toJSONString(equipmentDeviceEntity), Map.class);

        return R.success(result, "操作成功");
    }

    @ApiOperation("添加")
    @PostMapping(value = "/add")
    public void add(@Validated @RequestBody EquipmentDeviceAddParamModel paramModel) {
        //方式一：适合单一添加
        EquipmentDeviceEntity equipmentDeviceEntity = new EquipmentDeviceEntity();
        equipmentDeviceEntity.setData(paramModel);
        equipmentDeviceService.save(equipmentDeviceEntity);

        //方法二：适合事务添加
//        equipmentDeviceService.add(paramModel);
    }

    @ApiOperation("修改")
    @PostMapping(value = "/edit")
    public void edit(@Validated @RequestBody EquipmentDeviceEditParamModel paramModel) {
        //校验参数
        EquipmentDeviceEntity equipmentDeviceEntity = equipmentDeviceService.getById(paramModel.getId());
        if (equipmentDeviceEntity == null) {
            throw new ApiException("数据不存在，请刷新重试");
        }

        //方式一：适合单一编辑
        equipmentDeviceEntity.setData(paramModel);
        equipmentDeviceService.updateById(equipmentDeviceEntity);

        //方法二：适合事务编辑
//        equipmentDeviceService.edit(paramModel);
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delAllByIds(@PathVariable String[] ids) {
        //适合事务删除
        equipmentDeviceService.dels(ids);
    }
}